<template>
	<div class="app-container">
		<div class="row3">
			<span class="label">名称</span>
			<el-input v-model="queryData.productName" style="width: 350px; margin: 0 20px" placeholder="请输入名称字" />
			<span class="label">更新时间</span>
			<el-date-picker
				style="width: 350px; margin: 0 20px"
				v-model="queryData.updateTime"
				value-format="YYYY-MM-DD HH:mm:ss"
				type="daterange"
				range-separator="-"
				start-placeholder="开始日期"
				end-placeholder="结束日期"></el-date-picker>
			<el-checkbox style="position: relative; top: 3px" true-label="1" false-label="" v-model="queryData.isRecommend" label="推荐" size="large" />

			<el-button type="primary" style="margin-left: 20px" @click="search">搜索</el-button>
			<el-button @click="reset">重置</el-button>
			<el-button @click="linkToAdd()">添加产品</el-button>
			<el-button @click="importProduct">导入产品</el-button>
		</div>
		<div class="row2"></div>

		<BaseTable v-loading="loading" :columns="columns" :dataList="dataList">
			<template #action="{ row }">
				<el-button type="primary" size="small" plain @click="toDetail(row.productId)">详情</el-button>
				<el-button type="primary" size="small" plain @click="toEdit(row.productId)">修改</el-button>
				<el-button type="primary" size="small" plain @click="deleteRow(row.productId)">删除</el-button>
			</template>
		</BaseTable>
		<pagination :total="total" v-model:page="queryData.pageNum" v-model:limit="queryData.pageSize" @pagination="search" />
		<!-- 用户导入对话框 -->
		<el-dialog title="产品导入" v-model="upload.open" width="400px" append-to-body>
			<el-upload
				ref="uploadRef"
				:limit="1"
				accept=".xlsx,.xls"
				:headers="upload.headers"
				:action="upload.url + '?updateSupport=' + upload.updateSupport"
				:on-progress="handleFileUploadProgress"
				:on-success="handleFileSuccess"
				:auto-upload="false"
				drag>
				<el-icon class="el-icon--upload"><upload-filled /></el-icon>
				<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
				<template #tip>
					<div class="el-upload__tip text-center">
						<div class="el-upload__tip"><el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据</div>
						<span>仅允许导入xls、xlsx格式文件。</span>
						<el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="downloadfile">下载模板</el-link>
						<br /><el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="showTemplate"
							>下载分类对照表</el-link
						>
					</div>
				</template>
			</el-upload>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitFileForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>
<script setup>
import { reactive } from 'vue'
import BaseTable from '@/components/BaseTable'
import { listProduct, delProduct, exportP } from '@/api/product/product'
import { getToken } from '@/utils/auth'
import { download } from '@/utils/request'
const router = useRouter()
const { proxy } = getCurrentInstance()
const { is_recommend } = proxy.useDict('is_recommend')
/*** 用户导入参数 */
const upload = reactive({
	// 是否显示弹出层（用户导入）
	open: false,
	// 弹出层标题（用户导入）
	title: '导入产品',
	// 是否禁用上传
	isUploading: false,
	// 是否更新已经存在的用户数据
	updateSupport: 0,
	// 设置上传的请求头部
	headers: { Authorization: 'Bearer ' + getToken() },
	// 上传的地址
	url: import.meta.env.VITE_APP_BASE_API + '/function/product/importData'
})
/** 下载模板操作 */
function importTemplate() {
	proxy.download('/function/product/export/template', {}, `user_template_${new Date().getTime()}.xlsx`)
}
const downloadfile = () => {
	download({ url: '产品导入模板.xlsx' }).then()
}
/** 下载模板操作 */
function showTemplate() {
	proxy.download('/function/product/classification', {}, `分类对照表.xlsx`)
}
/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
	upload.isUploading = true
}
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
	upload.open = false
	upload.isUploading = false
	proxy.$refs['uploadRef'].handleRemove(file)
	proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + '</div>', '导入结果', {
		dangerouslyUseHTMLString: true
	})
	search()
}
/** 提交上传文件 */
function submitFileForm() {
	proxy.$refs['uploadRef'].submit()
}
const cancel = () => {
	upload.open = false
}
const columns = ref([
	{ key: 1, label: `名称`, prop: 'productName', type: 'text', visible: true },
	{ key: 6, width: '150', label: `产品编号`, prop: 'productCode', type: 'text', visible: true },
	{ key: 2, width: '210', label: `产品分类`, prop: 'typeName', type: 'text', visible: true },
	{ key: 2, width: '120', label: `品牌`, prop: 'productBrand', type: 'text', visible: true },
	{ key: 3, width: '90', label: `是否推荐`, prop: 'isRecommend', type: 'select', data: is_recommend, visible: true },
	{ key: 4, width: '150', label: `更新时间`, prop: 'updateTime', type: 'dateTimeSf', visible: true },
	{ key: 5, width: '220', label: `操作`, prop: 'action', type: 'custom', visible: true }
])
const total = ref(0)
const dataList = ref([])
const loading = ref(false)
const queryData = reactive({
	pageNum: 1,
	pageSize: 100,
	productName: '',
	updateTime: [],
	isRecommend: ''
})
const search = () => {
	let obj = { ...queryData }
	if (queryData.updateTime && typeof queryData.updateTime == 'object') {
		obj.startTime = queryData.updateTime[0]
		obj.endTime = queryData.updateTime[1]
		obj.updateTime = undefined
	}
	loading.value = true
	listProduct(obj).then((res) => {
		dataList.value = res.rows
		total.value = res.total
		loading.value = false
	})
}
search()
const reset = () => {
	for (let item in queryData) {
		queryData[item] = ''
	}
	queryData['updateTime'] = []
	queryData.pageNum = 1
	queryData.pageSize = 1000
	queryData.isRecommend = ''
	search()
}
const linkToAdd = () => {
	router.push({
		path: '/product/productAdd'
	})
}
const importProduct = () => {
	upload.open = true
}
const toDetail = (id) => {
	router.push({
		path: '/product/productDetail',
		query: { id: id }
	})
}
const toEdit = (id) => {
	router.push({
		path: '/product/productEdit',
		query: { id: id }
	})
}
const deleteRow = (id) => {
	proxy.$modal
		.confirm('是否确认此操作？')
		.then(() => {
			return delProduct(id)
		})
		.then(() => {
			search()
			proxy.$modal.msgSuccess('删除成功')
		})
		.catch(() => {})
}
</script>
<style lang="scss" scoped>
.left-label {
	color: #33b6fe;
	height: 24px;
	line-height: 24px;
	position: relative;
	top: 1px;
	margin-right: 10px;
	display: inline-block;
	min-width: 80px;
}
:deep(.el-table__header) {
	background-color: #2f3f56;
}
.row1 {
	border-bottom: 1px rgba(240, 240, 240, 1) solid;
	padding: 5px 0 10px 0;
	.label {
		padding: 8px 5px;
		font-size: 14px;
		background-color: rgb(231, 231, 231);
	}
	.select {
		width: 120px;
		position: relative;
		top: -1px;
	}
}
.row2 {
	border-bottom: 1px rgba(240, 240, 240, 1) solid;
	padding: 5px 0 10px 0;
}
.row3 {
	border-bottom: 1px rgba(240, 240, 240, 1) solid;
	padding: 5px 0 10px 0;
}
.row4 {
	padding: 5px 0 10px 0;
}
</style>
